package com.sc.test.mytest;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.provider.Telephony;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;

import com.sc.test.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-06-02-002 下午 15:38.
 */

public class MytestActivity extends AppCompatActivity {
    @BindView(R.id.bt_test)
    Button btTest;
    @BindView(R.id.bt_test2)
    Button btTest2;
    @BindView(R.id.viewpager)
    Button btTest3;
    @BindView(R.id.card)
    Button btCard;
    @BindView(R.id.btn4)
    Button btn4;

    private int height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mytest);
        ButterKnife.bind(this);

//        FirstTest.print();
        btTest.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                height = btTest.getHeight();
                //OnGlobalLayoutListener可能会被多次触发，因此在得到了高度之后，
                //要将OnGlobalLayoutListener注销掉
                btTest.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                Log.d("--->", height + "");


            }
        });

        btTest2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                int[] point = new int[2];
//                btTest2.getLocationInWindow(point);
//                int i = point[0];
//                int[] point1 = new int[2];
//                btTest2.getLocationOnScreen(point1);
//                int j = point1[0];
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });

        btTest3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MytestActivity.this, ViewPagerActivity.class);
                startActivity(intent);
            }
        });

        btCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MytestActivity.this, CardActivity.class);
                startActivity(intent);
            }
        });


        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent();
//                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                mIntent.setData(Uri.fromParts("package", getPackageName(), null));
                startActivityForResult(mIntent, 10);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 10) {
            System.out.println("123");
        }
    }

    public static final String[] PERMISSIONS = {Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS};

    @Override
    protected void onStart() {
        super.onStart();
//        Log.d("--->", height + "");
    }

    @Override
    protected void onResume() {
        super.onResume();
//        Log.d("--->", height + "");
    }


    @OnClick(R.id.bt_test)
    public void onViewClicked() {
//        String path = "/sdcard/1/a.jpg";
//        String b1path = "/sdcard/1/a1.jpg";
//        String b2path = "/sdcard/1/a2.jpg";
//        //加载本地图片
//        BitmapFactory.Options options=new BitmapFactory.Options();
//        options.inJustDecodeBounds=true;
//        BitmapFactory.decodeFile(path, options);
//        //获取图片的宽高
//        int outWidth = options.outWidth;
//        int outHeight = options.outHeight;
//        //获取手机屏幕宽高
//        Display display = getWindow().getWindowManager().getDefaultDisplay();
//        int width = display.getWidth();
//        int height = display.getHeight();
//        //定义一个默认的比率值
//        int scale=0;
//        int scaleX=outWidth/width;
//        int scaleY=outHeight/height;
//        //得到一个大的比率值
//        scale=scaleX>scaleY?scaleX:scaleY;
//        //设置比例
//        options.inSampleSize=scale;
//        //加载图片内容
//        options.inJustDecodeBounds=false;
//        //按照比例进行压缩
//
//        try {
//            Bitmap bitmap1 = BitmapFactory.decodeFile(path);
//            File file1 = new File(b1path);
//            FileOutputStream fos1 = new FileOutputStream(file1);
//            bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, fos1);
//            Log.d("----->1", bitmap1.getByteCount() + "");
//
//            Bitmap bitmap2 = BitmapFactory.decodeFile(path, options);
//            File file2 = new File(b2path);
//            FileOutputStream fos2 = new FileOutputStream(file2);
//            bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, fos2);
//            Log.d("----->2", bitmap2.getByteCount() + "");
//
//            Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }

        Intent mIntent = new Intent();
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        mIntent.setData(Uri.fromParts("package", getPackageName(), null));
        startActivity(mIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        Log.d("--->", "stop mars xlog");
//        LogUtil.getInstance().saveAndClose();
    }
}
